package cn.wegoteam.shop.po;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Where;

import cn.wegoteam.shop.cache.Cache;
import cn.wegoteam.shop.cache.StaticDataCache;
import cn.wegoteam.shop.util.Const;
import cn.wegoteam.shop.util.DateFormartUtil;

/**
 * Torder entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "torder", catalog = "shop")
public class Order extends BasePo implements java.io.Serializable {

	// Fields

	private Integer id;
	private Timestamp createDate;
	private String payDate;
	private User user;
	private String message;
	private String state; //订单状态
	private String payMethod;
	private String tradeno;
	private String orderNumber;
	private Float total;//总金额
	private Float freight;//运费
	private String address;//收货地址
	private String postcode;// 收货地址邮编
    private String accepter;//收货人
    private String phone;//电话号码
    private String telephone;//手机号码
	private String logistics;//物流信息
	private String logisticsNum;//物流编号
	private Integer bounds;//使用积分 
	
	private Set<Cart> carts;
	private int flag;

	
	/**
	 * 获取state对应的staticdata
	 * @return
	 */
	private Staticdata staticState;
	@Transient
	public Staticdata getStaticState() {
		if(this.staticState==null)
			this.staticState=StaticDataCache.getStaticdata(this.state);
		return this.staticState;
	}
	@Transient
	public String getLogisticsName() {
		return StaticDataCache.getStaticdata(this.logistics).getName();
	}
	
	private List<Staticdata> operates;
	@Transient
	public List<Staticdata> getOperates() {
		if(this.operates==null)
			this.operates=StaticDataCache.getStaticdatas(this.state, Const.NO);
		return this.operates;
	}
	public void clearOperates(){
		this.operates=null;
	}

	/** default constructor */
	public Order() {
	}


	// Property accessors
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "createDate", nullable = false, insertable = false, updatable = false)
	public Timestamp getCreateDate() {
		return this.createDate;
	}

	public void setCreateDate(Timestamp createDate) {
		this.createDate = createDate;
	}

	@Column(name = "payDate", length = 32)
	public String getPayDate() {
		return this.payDate;
	}

	public void setPayDate(String payDate) {
		this.payDate = payDate;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "uid", nullable = false)
	public User getUser() {
		return this.user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Column(name = "message", length = 100)
	public String getMessage() {
		return this.message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Column(name = "state", nullable = false, insertable=false, length = 15)
	public String getState() {
		return this.state;
	}
	public void setState(String state) {
		this.state = state;
	}

	@Column(name = "payMethod",nullable = false, length = 15, updatable = false)
	public String getPayMethod() {
		return this.payMethod;
	}

	public void setPayMethod(String payMethod) {
		this.payMethod = payMethod;
	}
	@Column(name = "logistics",length = 15)
	public String getLogistics() {
		return this.logistics;
	}

	public void setLogistics(String logistics) {
		this.logistics = logistics;
	}
	
	@Column(name = "logisticsNum",length = 30)
	public String getLogisticsNum() {
		return this.logisticsNum;
	}

	public void setLogisticsNum(String logisticsNum) {
		this.logisticsNum = logisticsNum;
	}
	
	@Column(name = "tradeno", length = 50)
	public String getTradeno() {
		return this.tradeno;
	}

	public void setTradeno(String tradeno) {
		this.tradeno = tradeno;
	}

	@Column(name = "orderNumber", nullable = false, length = 45)
	public String getOrderNumber() {
		return this.orderNumber;
	}

	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}

	@Column(name = "address", nullable = false, length = 200)
	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	@Column(name = "bounds", nullable = false,updatable=false)
	public Integer getBounds() {
		return this.bounds;
	}

	public void setBounds(Integer bounds) {
		this.bounds = bounds;
	}
	
	@Column(name = "phone", length = 20)
	public String getPhone() {
		return this.phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	@Column(name = "telephone", length = 20)
	public String getTelephone() {
		return this.telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	@Column(name="flag",insertable=false,nullable=false)
	public Integer getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	@Column(name = "accepter", nullable = false, length = 20)
	public String getAccepter() {
		return this.accepter;
	}

	public void setAccepter(String accepter) {
		this.accepter = accepter;
	}

	@Column(name = "postcode", nullable = false, length = 10)
	public String getPostcode() {
		return this.postcode;
	}

	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}
	
	/**
	 * 不能插入，只能更新，详情请参考添加action
	 * @return
	 */
	@Column(name = "total", nullable = false,insertable=false, precision = 0, scale = 0)
	public Float getTotal() {
		return this.total;
	}

	public void setTotal(Float total) {
		this.total = total;
	}
	@Column(name = "freight", nullable = false, precision = 0, scale = 0)
	public Float getFreight() {
		return this.freight;
	}

	public void setFreight(Float freight) {
		this.freight = freight;
	}
	
	@OneToMany(fetch = FetchType.EAGER, mappedBy = "order")
	@Where(clause="flag!=-100")
	public Set<Cart> getCarts() {
		return this.carts;
	}

	public void setCarts(Set<Cart> carts) {
		this.carts = carts;
	}
	@Transient
	public String getShowDate(){
		return DateFormartUtil.getDateByFormat(this.createDate,"yyyy-MM-dd");
	}
	@Transient
	public String getFreAndTotal(){
		DecimalFormat df = new DecimalFormat("#.00");
		return df.format(freight+total-bounds/Integer.parseInt(Cache.getSetting(Const.JIFEN).getValue()));
	}
	@Transient
	public String getShowPayMethod(){
		return StaticDataCache.getStaticdata(this.payMethod).getName();
	}
	

}